// import aq from "express"

const express=require("express");
const Blob=require("blob-polyfill")
const fs = require("fs");
const util = require("util");
const app=express();
const router=express.Router();
const mysql=require("mysql");
const Connection = require("mysql/lib/Connection");
let bodyParser = require('body-parser');
const { unescape } = require("querystring");
const { count } = require("console");


global['Blob'] = Blob


app.use(express.urlencoded({extended:false}))


app.use(bodyParser.json())


router.post("/bb/bbb",(req,res) => {console.log("router1");res.send("router1")});
router.get("/view/:namee",(req,res) => {
 
    req.setEncoding('utf8')
    
    res.setHeader('Content-Type','text/html; charset=utf-8');
    res.writeHead(200, {
            'Content-Type': 'text/html;charset=utf-8',
            'Content-Length': 99
          }).end(unescape(req.url.slice(6,req.url.length)));//链式调用
    
    // res.send(req.url.slice(6,req.url.length))
})

app.use("/bb",(req,res,next) => {console.log("asdbc");next();})

app.use(function (req, res,next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.setHeader("Access-Control-Allow-Methods", ["POST","GET","OPTIONS"])

next()
});

app.use(router);

app.use("/aa",(req,res,next) => {
console.log("res");
next()

})

app.post("/login",(req,res) => {
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        }
);
    connection.connect();
    connection.query(`select * from user where account=${req.body.account} and password=${req.body.password}`,(error,results,fields) => {
        res.send(results)
    })
    // connection.commit("insert into asdbc values(1,1,1,1,"+fs.readFileSync("./drink1.png")+")")
    connection.end();
})

app.post("/getUser",(req,res) => {
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        }
);
    connection.connect();
    connection.query('select count(*) as "count" from user where account='+req.body.account,(error,results,fields) => {
        res.send(results)
    })
    // connection.commit("insert into asdbc values(1,1,1,1,"+fs.readFileSync("./drink1.png")+")")
    connection.end();
})

app.post("/register",(req,res) => {
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        }
);
    connection.connect();
    
        connection.commit(`insert into user(account,password,nick_name,sex) values ('${req.body.account}','${req.body.password}','${req.body.nick_name}','${req.body.sex}')`)

    
    res.send(" ")
    // connection.commit("insert into asdbc values(1,1,1,1,"+fs.readFileSync("./drink1.png")+")")
    connection.end();
})

app.post("/updateUserInfo",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.commit(`update user set account='${req.body.account}',password='${req.body.password}',nick_name='${req.body.nick_name}',sex='${req.body.sex}' where account='${req.body.account}'`)
        
        res.send(" ")
        connection.end()
})

app.get("/getAll",(req,res) => {
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        }
);
    connection.connect();
    connection.query('select * from asdbc order by name desc',(error,results,fields) => {
        //fs.writeFileSync("./tt.png",results[0].image)
        //console.log(results)
        
        let arr=[]
        for(let i=results.length;i>0;i--){
            let obj={
                name:results[i-1].name,
                describe:results[i-1].describe,
                price:results[i-1].price,
                image:results[i-1].image,
            }
            arr.push(obj)
        }
        res.send(arr)})
    // connection.commit("insert into asdbc values(1,1,1,1,"+fs.readFileSync("./drink1.png")+")")
    connection.end();
})


app.post("/insertItem",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
                //请求体若为undefined,需要在前台传输配置content-type为application/json,并用app.use(bodyParser.json())
    connection.connect();
    connection.commit("insert into items_list values( '"+req.body.name+"','"+req.body.detail+"','"+req.body.price+"','"+req.body.number+"','"+req.body.image_name+"','"+req.body.shop_name+"','"+req.body.shop_addr+"' ) ")
   
    // res.send(req.body);//post需要引入body-parser,接收参数需要使用req.body/req.params/req.query
    
    res.send(" ")
    connection.end();
})
app.get("/getItems",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
                //请求体若为undefined,需要在前台传输配置content-type为application/json,并用app.use(bodyParser.json())
    connection.connect();
    connection.query("select * from items_list",(error,results,field)=>{
        res.send(results)
    })
    connection.end();
})
app.get("/clearItems",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.commit("delete from items_list ")
        res.send(" ")
        connection.end()

})
app.use(bodyParser.json())

app.post("/insertPay",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        for(let i=0;i<req.body.length;i++){
            if(req.body[i].num!=0){
                connection.commit("insert into pay_list values('"+req.body[i].name+"','"
            +req.body[i].detail+"',"
            +req.body[i].num+","
            +req.body[i].price+",'"
            +req.body[i].image_name+"','"
            +req.body[i].shop_name+"','"
            +req.body[i].shop_addr+"','"
            +"在这吃'"+")"
            )
            }
            
        }
        
        res.send(" ")
        connection.end()
})
app.get("/getPay",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.query("select * from pay_list ",(a,results,b)=>{
            res.send(results)
        })
        connection.end()

})
app.get("/delPay",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.commit("delete from pay_list")
        res.send(" ")
        connection.end()
})

app.post("/insertOrder",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        
        for(let i=0;i<req.body.length;i++){
                
                connection.commit("insert into order_list values('"+req.body[i].name+"','"
                    +req.body[i].detail+"',"
                    +req.body[i].num+","
                    +req.body[i].price+",'"
                    +req.body[i].image_name+"',"
                    +req.body[i].order_id+",'"
                    +req.body[i].shop_name+"','"
                    +req.body[i].shop_addr+"','"
                    +"在这吃'"+")"
            )
            
            
        }
        res.send(" ")
       
        connection.end()
})
app.get("/getOrder",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.query("select * from order_list ",(a,results,b)=>{
            res.send(results)
        })
        connection.end()

})
app.get("/delOrder",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.commit("delete from order_list")
        res.send(" ")
        connection.end()
})
//不管什么,一定要res.send,不然uni.request方法调用会阻塞,并且success回调方法不会被调用
app.get("/getHistoryOrders",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.query("select * from history_order_list ",(a,results,b)=>{
            res.send(results)
        })
        connection.end()

})
app.post("/insertHistoryOrder",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        
        for(let i=0;i<req.body.value.length;i++){
            
            connection.commit("insert into history_order_list values('"+(req.body.value)[i].name+"','"
                +(req.body.value)[i].detail+"',"
                +(req.body.value)[i].num+","
                +(req.body.value)[i].price+",'"
                +(req.body.value)[i].image_name+"',"
                +(req.body.value)[i].order_id+",'"
                +(req.body.value)[i].shop_name+"','"
                +(req.body.value)[i].shop_addr+"','"
                +"在这吃'"
                +")"
            )     
        }
        res.send(" ")
       
        connection.end()
})
app.post("/setStore",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.commit("insert into store values('"+req.body.name+`',${req.body.account})`)
        console.log(req.body)
        connection.end()
        res.send(" ")
    }
            )
app.post("/delStore",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.commit("delete from store where shop_name='"+req.body.name+"' and user="+req.body.account)
        connection.end()
        res.send(" ")
    }
 )
 app.post("/getStore",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.query("select shop_name from store where user= "+req.body.account,(a,results,b)=>{
            console.log(results)
            res.send(results)
        })
        connection.end()
    }
 )
 app.get("/getCurrentOrders",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.query("select * from current_order_list ",(a,results,b)=>{
            res.send(results)
        })
        connection.end()

})
 app.post("/insertCurrentOrder",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        for(let i=0;i<req.body.length;i++){
            
                connection.commit("insert into current_order_list values('"+req.body[i].name+"','"
                    +req.body[i].detail+"',"
                    +req.body[i].num+","
                    +req.body[i].price+",'"
                    +req.body[i].image_name+"',"
                    +req.body[i].order_id+",'"
                    +req.body[i].shop_name+"','"
                    +req.body[i].shop_addr+"','"
                    +"在这吃"+"','"
                    +"制作中'"
                    +")"
            )     
        }
        connection.end();
        res.send(" ")
})
app.post("/finishCurrentOrder",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        console.log(req.body)
        connection.commit("update current_order_list set status='已完成' where order_id="+req.body.msg);
        
        connection.end();
        res.send(" ")
})
app.post("/delCurrentOrder",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        console.log(req.body)
        connection.commit("delete from current_order_list where order_id="+(req.body.value)[0].order_id);
        
        connection.end();
        res.send(" ")
})
app.post("/addItem",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        console.log(req.body) 
        let temp;
        connection.commit(`insert into asdbc values('${req.body.name}','${req.body.describe}',${req.body.price},'${req.body.image.path}')`);

        connection.end();
        res.send(" ")
})
app.post("/delItem",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        console.log(req.body) 
        let temp;
        connection.commit(`delete from asdbc where name='${req.body.name}'`);

        connection.end();
        res.send(" ")
})
app.post("/updateItem",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        console.log(req.body) 
        let temp;
        connection.commit(`update asdbc a set a.name='${req.body.name}',a.describe='${req.body.describe}',a.price=${req.body.price},a.image='${req.body.image}' where a.name='${req.body.oldName.oldName}'`)
        connection.end();
        res.send(" ")
}) 



 app.get("/getStatus",(req,res)=>{
    const connection=mysql.createConnection(
        {
            host:'127.0.0.1',
            database:'asdbc',
            user:'root',
            port: 3306,
            password: '123456',
        })
        connection.connect();
        connection.query("select distinct order_id from current_order_list ",(a,results,b)=>{
            res.send(results)
        })
        connection.end()

 })
app.post("/fuck",(req,res) => {res.send('get')})

// app.get("/aa",(req,res) => {res.send('aaa')});
app.listen(3000,() => {console.log("running")});